#!/usr/bin/env groovy

pipeline {
    agent any
    environment {
        ANDROID_HOME = '/Users/jenkins/Library/Android/sdk'
        ANDROID_BUILD_TOOLS_VERSION = '30.0.3'
        ANDROID_CONNECTED_TEST_RETRY_COUNT = 3

        APK_INTERNAL_RELEASE_SIGNED = 'mopub-sample/build/outputs/apk/internal/release/mopub-sample-internal-release-signed.apk'
        APK_INTERNAL_RELEASE_UNSIGNED = 'mopub-sample/build/outputs/apk/internal/release/mopub-sample-internal-release-unsigned.apk'
        APK_INTERNAL_RELEASE_UNSIGNED_ALIGNED = 'mopub-sample/build/outputs/apk/internal/release/mopub-sample-internal-release-unsigned-aligned.apk'

        APK_EXTERNAL_RELEASE_SIGNED = 'mopub-sample/build/outputs/apk/external/release/mopub-sample-external-release-signed.apk'
        APK_EXTERNAL_RELEASE_UNSIGNED = 'mopub-sample/build/outputs/apk/external/release/mopub-sample-external-release-unsigned.apk'
        APK_EXTERNAL_RELEASE_UNSIGNED_ALIGNED = 'mopub-sample/build/outputs/apk/external/release/mopub-sample-external-release-unsigned-aligned.apk'

        projectName = """${
            sh(script: 'IFS="/" read -ra TOKENS <<< "${JOB_NAME}"; echo ${TOKENS[0]}', returnStdout: true).trim()
        }"""
        PARSED_JOB_NAME = URLDecoder.decode(env.JOB_NAME, 'UTF-8')
    }

    stages {
        stage('Build AARs') {
            steps {
                echo "Building the MoPub SDK AARs"
                sh '''
                    #!/bin/bash
                    ./gradlew clean
                    ./gradlew :mopub-sdk:build -x test
                    ./gradlew :mopub-sdk:mopub-sdk-base:build -x test
                    ./gradlew :mopub-sdk:mopub-sdk-banner:build -x test
                    ./gradlew :mopub-sdk:mopub-sdk-fullscreen:build -x test
                    ./gradlew :mopub-sdk:mopub-sdk-native-static:build -x test
                    ./gradlew :mopub-sdk:mopub-sdk-networking:build -x test
                    ./gradlew :mopub-sdk:mopub-sdk-util:build -x test
                '''
            }
        }
        stage('Build APKs') {
            steps {
                echo "Building the MoPub Sample App"
                sh '''
                    #!/bin/bash
                    ./gradlew :mopub-sample:build -x test
                  '''
            }
        }
        stage('Internal automation tests') {
            steps {
                echo "Internal Automation Tests are running - ${PARSED_JOB_NAME}"
                sh '''
                    #!/bin/bash
                    chmod +x scripts/private/androidRT.sh
                    scripts/private/androidRT.sh
                  '''
            }
        }
        stage('External automation tests') {
            steps {
                echo "External Automation Tests are running - ${PARSED_JOB_NAME}"
                sh '''
                    #!/bin/bash
                    chmod +x scripts/private/android.sh
                    scripts/private/android.sh
                  '''
            }
        }

        stage('Sign apk Internal') {
            steps {
                // Remove old APKs generated by this step
                sh 'rm -f $APK_INTERNAL_RELEASE_SIGNED'
                sh 'rm -f $APK_INTERNAL_RELEASE_UNSIGNED_ALIGNED'

                // Create unsigned and aligned APK from unsigned APK
                sh '$ANDROID_HOME/build-tools/$ANDROID_BUILD_TOOLS_VERSION/zipalign -v -p 4 $APK_INTERNAL_RELEASE_UNSIGNED $APK_INTERNAL_RELEASE_UNSIGNED_ALIGNED'

                // Use credentials to create signed APK from unsigned and aligned APK
                withCredentials([string(credentialsId: 'android_store_key_pass', variable: 'JKS_PASS')]) {
                    sh '$ANDROID_HOME/build-tools/$ANDROID_BUILD_TOOLS_VERSION/apksigner sign --ks ~/google_play_key.jks --ks-pass pass:$JKS_PASS --out $APK_INTERNAL_RELEASE_SIGNED $APK_INTERNAL_RELEASE_UNSIGNED_ALIGNED'
                }
            }
        }
        stage('Sign apk External') {
            steps {
                // Remove old APKs generated by this step
                sh 'rm -f $APK_EXTERNAL_RELEASE_SIGNED'
                sh 'rm -f $APK_EXTERNAL_RELEASE_UNSIGNED_ALIGNED'

                // Create unsigned and aligned APK from unsigned APK
                sh '$ANDROID_HOME/build-tools/$ANDROID_BUILD_TOOLS_VERSION/zipalign -v -p 4 $APK_EXTERNAL_RELEASE_UNSIGNED $APK_EXTERNAL_RELEASE_UNSIGNED_ALIGNED'

                // Use credentials to create signed APK from unsigned and aligned APK
                withCredentials([string(credentialsId: 'android_store_key_pass', variable: 'JKS_PASS')]) {
                    sh '$ANDROID_HOME/build-tools/$ANDROID_BUILD_TOOLS_VERSION/apksigner sign --ks ~/google_play_key.jks --ks-pass pass:$JKS_PASS --out $APK_EXTERNAL_RELEASE_SIGNED $APK_EXTERNAL_RELEASE_UNSIGNED_ALIGNED'
                }
            }
        }

        stage('Archive') {
            steps {
                archiveArtifacts artifacts: 'mopub-sample/build/outputs/**/*.apk', excludes: 'mopub-sample/build/outputs/**/*unsigned.apk, mopub-sample/build/outputs/**/*aligned.apk', onlyIfSuccessful: true
                archiveArtifacts artifacts: 'mopub-sdk/build/outputs/aar/mopub-sdk-*.aar', onlyIfSuccessful: true
                archiveArtifacts artifacts: 'mopub-sdk/mopub-sdk-*/build/outputs/aar/mopub-sdk-*.aar', onlyIfSuccessful: true
            }
        }
    }

    post {
        fixed {
            slackSend color: 'GREEN', message: "<${env.BUILD_URL}|${PARSED_JOB_NAME} #${env.BUILD_NUMBER}> has succeeded."
        }
        failure {
            slackSend color: 'RED', message: "Attention @here <${env.BUILD_URL}|${PARSED_JOB_NAME} #${env.BUILD_NUMBER}> has failed."
        }
    }
}
